<?php

global $confirmType;
global $actionType;
global $organizationType;
foreach($pendingTransactions as $trans){
		$id=$trans->id;
		$action=$actionType[$trans->action];
    	$source=$trans->source;
    	$target=$trans->target;
    	$confirm=$trans->confirm;
    	$confirmName=$confirmType[$confirm];
    	$user1=$trans->user1;
    	$user2=$trans->user2;
    	$code=$trans->code;
    	if($option=='checkbox'){
    		$user2="无";
    	}
	    $scan_code=$trans->scan_code;
    	$amount=$trans->amount;
    	$income=$trans->income;
	    $begin_time_stamp=$trans->begin_time_stamp;
	    $finish_time_stamp=$trans->finish_time_stamp;
		
}
//return;
?>

<script>
var userid="<?php echo $currentUser->id;?>";
var orgid="<?php echo $organization->id; ?>";
var myRequest=new stackRequest("organization_distribute_confirm",userid,orgid,new Array());
function load(){
	jQuery.ajax({
				  type: "POST",
				  url: "api/",
				  dataType : 'json',   
				  data:{	action:'get_transaction',
							userid:myRequest.userid,
							orgid:myRequest.orgid,
							type:'distribute'},
			  	  beforeSend: function ( xhr ) {
			 		$('.dim').show();
				 	},
				  complete: function(jqXHR) {
				  	   if(jqXHR.readyState != 4) {
				  	   	alert(服务器通讯或者运算出现问题);
				  	   		 $('.dim').hide();
      					 }  
				  	//alert(data['stat']);
				  
 
	    			}        
			}).done(function ( data ) {
				$('.dim').hide();

				if(data['stat']==0){
					alert('递交参数不完整');

				}else{
					myRequest.addInventorys(data.inventorys);
					myRequest.printPending();
				}
			});
}


function scan(){
	var source=$('#source option:selected').html();
	if($('#count_scan').val()!=""){
		var a= myRequest.complxscan($('#count_scan').val(),source);
		var message="您刚刚扫描了"+$('#count_scan').val()+"，来源设为"+source;
		if(a==0){
			alert("所读条形码 "+$('#count_scan').val()+"不在来自 "+source+"的待收分配表里\n请重新扫描或选择来自其他机构");
		$('#history').html("<div class='isa_error'><h4>扫描操作失败：</h4>"+message+"</div>");
		}else if(a==-1){
			alert("所读条形码 "+$('#count_scan').val()+"已盘点过的数目已经等于计划数目\n请重新扫描");
		}else{
			//$('#count_scan').focus();
		
			$('#history').html("<div class='isa_success'><h4>扫描成功：</h4>"+message+"</div>");
		}

		$('#count_scan').val("");
		$('#count_scan').focus();
	}
	
}


function stack(){
	var ready=myRequest.isPartiallyReady();
	if(!ready){
		var r=confirm("发现有些您完成了一半的盘点，若选择“OK”则会递交你已经完成的盘点，而“Cancel”则什么都不会递交");
		if (r==true)
		  {
			
		  }
		else
		  {
		    return;
		  }

	}
	var inv=new Array();
	
		jQuery.each(myRequest.inventorys, function() {
			if(this.num2>0&&this.num2==this.num1){
				inv.push(this);
			}
		});
	console.log(inv);
			jQuery.ajax({
				  type: "POST",
				  url: "api/",
				  dataType : 'json',   
				  data:{	action:myRequest.action,
							userid:myRequest.userid,
							orgid:myRequest.orgid,
							inventorys:inv},
			  	  beforeSend: function ( xhr ) {
			 		$('.dim').show();
				 	},
				  complete: function(jqXHR) {
				  	   if(jqXHR.readyState != 4) {
				  	   	alert(服务器通讯或者运算出现问题);
				  	   		 $('.dim').hide();
      					 }  

	    			}        
			}).done(function ( data ) {
								 $('.dim').hide();

				if(data['stat']==0){
					alert('递交参数不完整');

				}else{
					var stackedNum=data['stackedNum'];
					var failededNum=data['failededNum'];
					var error=data['error'];
					var errorName="";
					for (var i = 0; i < error.length; i++) {
						errorName+="\n"+error[i];
					}
					alert("成功接收了"+stackedNum+"种产品,失败了 "+failededNum+"产品。"+errorName);
				}
			});
			myRequest.dump();
			document.location.reload(true);

}

$(document).ready(function() {
    load();

});

</script>

	<div class="block">

	<div class="invertedshiftdown">
<ul>
	<?php
	if($organization->type=='s'){
	?>
			<li><a href="?page=organization_inventory&id=<?php echo $organization->id; ?>">查看货存</a></li>
			<li><a href="?page=organization_sell&id=<?php echo $organization->id; ?>">出售</a></li>
			<li><a href="?page=organization_return&id=<?php echo $organization->id; ?>">退款</a></li>
			<li><a href="?page=organization_distribute&id=<?php echo $organization->id; ?>">分配</a></li>
			<li class="current"><a href="?page=organization_distribute_confirm&id=<?php echo $organization->id; ?>">处理待定分配</a></li>
			<li><a href="?page=organization_transaction&id=<?php echo $organization->id; ?>">查看物流记录</a></li>
			<li><a href="?page=organization_sell_record&id=<?php echo $organization->id; ?>">查看销售记录</a></li>
			<li><a href="?page=organization_consume&id=<?php echo $organization->id; ?>">报告损耗/遗失</a></li>
			<li><a href="?page=organization_count&id=<?php echo $organization->id; ?>">货存盘点</a></li>
	<?php
	}elseif($organization->type=='w'){
	?>
	       	<li><a href="?page=organization_inventory&id=<?php echo $organization->id; ?>">查看库存</a></li>
			<li><a href="?page=organization_stack&id=<?php echo $organization->id; ?>">入库</a></li>
			<li><a href="?page=organization_distribute&id=<?php echo $organization->id; ?>">分配</a></li>
			<li class="current"><a href="?page=organization_distribute_confirm&id=<?php echo $organization->id; ?>">处理待定分配</a></li>
			<li><a href="?page=organization_transaction&id=<?php echo $organization->id; ?>">查看物流记录</a></li>
			<li><a href="?page=organization_consume&id=<?php echo $organization->id; ?>">报告损耗/遗失</a></li>
			<li><a href="?page=organization_count&id=<?php echo $organization->id; ?>">货存盘点</a></li>
    <?php
	}
	?>
</ul>




<form id="myform">
<input type="text" class="textinput" /> <input class="submit" type="submit" value="搜索" />
</form>
</div>

<br style="clear: both;" />
<div id= 'content'>

		<h2><?php echo $organization->name ?>》处理待收分配</h2>
		
		
		
		<?php
	//print_r($_GET);
	if(isset($_GET['stat'])){
		if($_GET['stat']=='confirmed'){
			
		
	
	?>
	<div class="isa_success">
		<h4>操作成功：</h4>分配确认成功
	</div>
	
	<?php
		}elseif($_GET['stat']=='error'&&$_GET['error']=='unknow'){
	?>
	<div class="isa_warning">
		<h4>操作失败：</h4>无法进行此次确认
	</div>
	
		<?php
		}elseif($_GET['stat']=='error'&&$_GET['error']=='badData'){
	?>
	<div class="isa_warning">
		<h4>操作失败：</h4>无法入库（提交了无效数据）
	</div>
<?php

		}
		echo "<br>";
}
?>
					<div id='history' style="padding-left:16px;">
						<div class="isa_info">
							<h4>接受分配小贴士：</h4>首先选定“货物来源” 然后开始进行一件件扫描盘点。最后按最下面的盘点完毕递交
							<p><div class="important">要接收到的货物的形码若没能在下面列表找到请联络管理员</div></p>
						</div>
					</div>
		   <div class="element" style="clear:both;">
            <label class="form" >货物来源：</label>
			   <select id="source" name="source" style="width:150px;">
	             <?php
	             foreach ($allOrganizations as  $org) {
	             	$id = $org->id;
	             	$name =$org->name;
					
					if($id!=$organization->id){
	             ?>
	             <option value='<?php echo $id;?>' ><?php echo $name;?></option>
	             <?php
	                }
	              }
	             ?>
	           </select>
             </div>
				<div style="clear: both;">

					<input id="count_scan" ype="text" maxlength="14" size="18">
					<div class="button blue" onclick="scan()">盘点扫描</div>

				</div>
          <br style="clear: both;" />
             
             <style type='text/css' title='currentStyle'>
					@import 'css/demo_page.css';
					@import 'css/demo_table.css';
				</style>
				<div class='tableBackground'>
			
			<table cellpadding='0' cellspacing='0' border='0' class='display' id='stackTable' width='100%'>
				  <caption style="padding-bottom: 10px"><h2>待收分配表</h2></caption>
				  

				<thead>
					<tr>
				      	<th>计划分配数量</th>
				      	<th>已扫描数量</th>
						<th>扫描条码</th>
						<th>来自</th>
						<th>操作员</th>
						<th>操作号码</th>
						<th>日期</th>
					</tr>
				</thead>
				<tbody>
					
				</tbody>
				<tfoot>
				  <tr>
				  		<th>计划分配数量</th>
				      	<th>已扫描数量</th>
						<th>扫描条码</th>
						<th>来自</th>
						<th>操作员</th>
						<th>操作号码</th>
						<th>日期</th>
				   </tr>
				</tfoot>
			</table>
			</div>
			<br>
						<div style="clear: both;">
							<div class="button blue" onclick="stack()">盘点完毕提交</div>
						</div>

<br><br><br>


   
<div class="clear"></div>
	</div>